
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Text;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;
using Swaf.Container;

namespace FilterSort
{
    public class FilterSortBizRules : Syrinx.BaseBizRules
	{
        /// <summary>
        /// insert or update a fileterSort BizObj
        /// </summary>
        /// <param name="app"></param>
        /// <param name="contenxt"></param>
        /// <param name="filterSort"></param>
        public void addUpdateFilterSortSave(IApplication app, IRuleContext contenxt, IBizObj filterSort)
        {
            if (filterSort == null)
                throw new FilterSortException("error executing addUpdateFilterSortSave filterSort IBizObj is null");
            try
            {
                if ((bool)filterSort.get("isCompanyLevel"))
                    filterSort.put("associatedId", CurrentCompanyId);
                else
                    filterSort.put("associatedId", (Swaf.BGuid.Guid)CurrentUser.get("id"));

                if (filterSort.isNew)
                {
                    filterSort.put("id", new Swaf.BGuid.Guid());
                    app.appData.put("DA.FilterSort.InsertFilterSortInfo", filterSort);
                }
                else
                    app.appData.put("DA.FilterSort.UpdateFilterSortInfo", filterSort);
            }
            catch (Exception e)
            {
                throw new FilterSortException("error executing addUpdateFilterSortSave",e);
            }
        }

        /// <summary>
        /// delete a filterSort
        /// </summary>
        /// <param name="app"></param>
        /// <param name="context"></param>
        /// <param name="id">id of the filtersort</param>
        public void deleteFilterSortSave(IApplication app, IRuleContext context, Swaf.BGuid.Guid id)
        {
            if (id == null)
                throw new FilterSortException("error executing deleteFilterSortSave filterSort IBizObj is null");
            try
            {
                FlexiMap m = new FlexiMap();
                m["id"] = id;
                app.appData.put("DA.FilterSort.DeleteFilterSortInfo", m);
            }
            catch (Exception e)
            {
                throw new FilterSortException("error executing deleteFilterSortSave",e);
            }
        }

        /// <summary>
        /// get the filterSort associated by a company id or a a user Id
        /// </summary>
        /// <param name="app"></param>
        /// <param name="context"></param>
        /// <param name="id">assocaitedId</param>
        /// <returns></returns>
        public IBizObjList getFilterSortSaves(IApplication app, IRuleContext context, string groupName)
        {
            try
            {
                FlexiMap m = new FlexiMap();

                m["groupName"]=groupName;
                m["userId"] = CurrentUser.get("id");
                m["companyId"] = CurrentCompanyId;
                IBizObjList lst = (IBizObjList)app.appData.get("DA.FilterSort.GetFilterSortSaves", m);
                
                // if list is blank provide an empty filtersort
                if (lst.Count == 0)
                {
                    IBizObj obj = lst.insertNew();
                    obj.put("associatedId",(Swaf.BGuid.Guid)CurrentUser.get("id"));
                }

                return lst;
            }
            catch (Exception e)
            {
                throw new FilterSortException("error executing getFilterSortSaves",e);
            }
        }

        /// <summary>
        /// get the details of a filterSort details
        /// </summary>
        /// <param name="app"></param>
        /// <param name="context"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public IBizObj getFilterSortSaveDetails(IApplication app, IRuleContext context, Swaf.BGuid.Guid id)
        {
            try
            {
                FlexiMap m = new FlexiMap();
                m["id"] = id;
                //m["id"] = new Swaf.BGuid.Guid("{6bc31081-d942-43f8-83e8-d07fd393d632}");
                IBizObjList lst = (IBizObjList)app.appData.get("DA.FilterSort.GetFilterSortSaveDetails", m);
                return (lst.Count > 0? lst[0]:null);
            }
            catch (Exception e)
            {
                throw new FilterSortException("error executing getFilterSortSaveDetails",e);
            }
        }

        /// <summary>
        /// get the filterSortGroupDetails
        /// </summary>
        /// <returns></returns>
        public IBizObj getFilterSortGroupDetails(IApplication app, IRuleContext context, string groupName)
        {
            FlexiMap lstGroupFilters = app.appData["filterSortOptions"] as FlexiMap;
            if (lstGroupFilters != null)
                return (IBizObj)lstGroupFilters.get(groupName);

            return null;
        }

        public IBizObjList getFilterFieldOptions(IApplication app, IRuleContext context, string groupName)
        {
            IBizObj obj = getFilterSortGroupDetails(app, context, groupName);
            if (obj == null)
                throw new FilterSortException("Error executing getFilterFieldOptions unable to resolve groupName value");
            else
            {
                IBizObjList lstObj =  (IBizObjList)obj.get("fieldOptions");
                return lstObj;
            }
        }

        public IBizObjList getFilterFieldOptionsCriteria(IApplication app, IRuleContext context, string groupName, string fieldName)
        {

            return null;
        }
    }
}